 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=243 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:36 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Command line operations
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Command line operations</div>

<div class='main-body'>
 
<br>
Most Java programmers use an Integrated Development Environment (IDE) such as IntelliJ, NetBeans, or Eclipse.
Modern IDEs are usually pleasant to use, and go a long way in simplifying things for a typical programmer. 

<P>However, being familiar with Java's basic <a href='http://java.sun.com/javase/6/docs/technotes/tools/index.html'>command line tools</a> is still very useful.
It not only gives you a better understanding of what's happening, it's essential in situations where you need to use the command line tools directly (scripts, tools, and so on).

<P>The most commonly used tools are :
<ul>
 <li><a href='http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html'>javac</a> - the java compiler
 <li><a href='http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html'>java</a> - launching programs on the Java Runtime Environment
 <li><a href='http://java.sun.com/javase/6/docs/technotes/tools/windows/javadoc.html'>javadoc</a> - generating documentation for your project
</ul>


<P>Here are some examples of running command line tools. It assumes a project with the following directory structure:
<PRE>
PROJECT_HOME
  -> lib (jar files)
  -> src (java code)
    -> hirondelle (top-level package; no .java files)
      -> ante (.java files)
        -> deluvian (.java files)
</PRE>

<h3>Compiling With javac</h3>
<PRE style='font-size:75%'>
PROJECT_HOME>javac -cp lib\* src\hirondelle\ante\*.java src\hirondelle\ante\deluvian\*.java
</PRE>

This compiles in place, and creates .class files beside .java files. 
If you want to place generated class files elsewhere, use the -d option to put them into an existing directory :
<PRE style='font-size:75%'>
PROJECT_HOME>javac -cp lib\* -d build src\hirondelle\ante\*.java src\hirondelle\ante\deluvian\*.java
</PRE>

Also, notice that all jars in a directory can be succinctly referenced using 'lib\*'.
However, referencing the source files that you need to compile is a different story: <em>all of your packages need to be listed one by one</em>.
That is, you can't simply specify a single root directory for your source files.
This is a rather serious defect of javac.

<P>If your jars are in various directories, then the classpath is a list delimited by semi-colons :
<PRE  style='font-size:75%'>
-cp lib\*;C:\abc\one.jar;C:\xyz\two.jar
</PRE>

<h3>Running With java</h3>
Any class having a main method may be run as a Java program. The main method must have the form :
<PRE>public static void main(String... aArgs){...}</pre>

An application is run by referencing a fully-qualified class name, but <em>without</em> the .class suffix.
Assuming that the classes have been compiled in place, our example application can be run using :
<PRE style='font-size:75%'>
PROJECT_HOME>java -cp lib\*;src hirondelle.ante.Launcher
</PRE>

Passing 3 arguments to the program : 
<PRE style='font-size:75%'>
PROJECT_HOME>java -cp lib\*;src hirondelle.ante.Launcher arg1 arg2 "Arg Three"
</PRE>

Setting a System property value with -D: 
<PRE style='font-size:75%'>
PROJECT_HOME>java -Dblah=whatever -cp lib\*;src hirondelle.ante.Launcher
</PRE>

Set the initial and maximum size of the object allocation pool to 5 Meg and 100 Meg, respectively : 
<PRE style='font-size:75%'>
PROJECT_HOME>java -cp lib\*;src -Xms5m -Xmx100m hirondelle.ante.Launcher
</PRE>

Run a jar file whose manifest specifies a Main-Class attribute :
<PRE style='font-size:75%'>
C:@builddist>java -jar example-app-1.1.jar
</PRE>

The javaw command is the same as the java command, with the only difference being that javaw has no associated console window. 


<h3>Using javadoc</h3>


<P>For files under the src directory, the following command will javadoc all packages starting with 'hirondelle.ante', and  
output the result to C:\@build\javadoc :
<PRE style='font-size:75%'>
javadoc -sourcepath src -subpackages hirondelle.ante -classpath lib\* -d C:\@build\javadoc
</PRE>

The javadoc tool has many options. You may find these especially useful:
<ul>
<li><i>-linksource</i>: links your javadoc to a static snapshot of the underlying source code (very nice).
<li><i>-windowtitle</i>, <i>-header</i> and <i>-footer</i>: often useful for showing the name and version of your application.
<li><i>-link</i>: link to Sun's javadoc as well
<li><i>-noqualifier</i>: remove tedious package prefixes from Sun classes
<li>define the minimum class scope to be processed, using one of: <i>-private</i>, <i>-package</i>, <i>-protected</i>, <i>-public</i>.
</ul>

Here's a second example, with more options :
<PRE style='font-size:75%'>
javadoc 
 -sourcepath src 
 -subpackages hirondelle.ante 
 -package
 -classpath lib\* 
 -d C:\@build\javadoc
 -linksource
 -link http://java.sun.com/javase/6/docs/api/
 -noqualifier java.*:javax.*
 -windowtitle "My App 1.0"
 -header "&lt;b&gt;My App 1.0&lt;/b&gt;"
 -footer "&lt;a href='http://www.blah.com'&gt;My App&lt;/a&gt;"
</PRE>


Javadoc can also be controlled using the following items, placed beside your source code:
<ul>
<li><i>package-info.java</i> - 
Each package can include a package-info.java file. It contains package-level documentation.
Despite its extension, a package-info.java file is <em>not a Java class file</em>. 
<li><i>doc-files</i> subdirectories - each package can include a 'doc-files' subdirectory, containing items that can be 
linked from javadoc (images, html files, and so on) using ordinary hypertext links. 
<li><i>overview.html</i> - a single file describing general aspects of your project. (This file doesn't necessarily reside 
in your source tree, but it makes sense to place it there, in analogy with the package-info.java files.)
</ul>

<br>

</div>





<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='243'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=243 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:36 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
